<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/6/4
 * Time: 13:21
 * 统计入库 统计某媒资分享总数
 * update: [lx][2019-01-29]
 */
require_once dirname(__DIR__) . "/common.php";

use app\lib\db\DbException;
use app\cls\tom\Queue;
use app\cls\tom\SysResource;
use app\cls\tom\Process;
use app\cls\tom\Tools;

$process = new Process(__FILE__);
$process->setProcessNum(5);
$process->initProcess();

$redis = SysResource::getRedis();
$db = SysResource::getDB(true);

try {
    $index = 0;
    while (true) {
        $index++;
        $runTimes = 200;
        if ($index > $runTimes) {
            $time = date('Y-m-d H:i:s');
            echo "[{$time}] 干完活了{$runTimes}次，退出!\n";
            break;
        }

        for ($i = 1; $i <= 5; $i++) {
            for ($j = 0; $j < 100; $j++) {
                $key = REDIS_PREFIX . "test_PV_" . $i;
                $dataStr = $redis->rPop($key);
                $result = json_decode($dataStr, TRUE);

                if (empty($result)) {
                    break;
                }
                $time = date('Y-m-d H:i:s');
                echo "[{$time}]\n缓存数据：{$dataStr}\n";

                if ($result && in_array($result['xm'], ['host'])) {
                    $data['log_uid'] = isset($result['uid']) ? $result['uid'] : '';
                    $data['material_id'] = isset($result['id']) ? $result['id'] : '';
                    $ip = isset($result['ip']) && $result['ip'] ? explode(",", $result['ip']) : null;
                    $data['log_ip'] = $ip && isset($ip[0]) && $ip[0] ? $ip[0] : '0.0.0.0';
                    $data['log_project'] = isset($result['xm']) && $result['xm'] ? $result['xm'] : '0';
                    $data['log_position'] = isset($result['wz']) ? $result['wz'] : '';
                    $data['log_platform'] = isset($result['pt']) ? $result['pt'] : '';
                    $data['log_operate'] = isset($result['op']) ? $result['op'] : '';
                    $data['log_uniqueid'] = isset($result['uniqueid']) ? $result['uniqueid'] : '';
                    $data['log_other'] = isset($result['other']) ? $result['other'] : '';
                    $data['log_time'] = isset($result['time']) && $result['time'] ? $result['time'] : date("Y-m-d H:i:s");
                    $data['log_addtime'] = date("Y-m-d H:i:s", time());
                    if (!empty($result['id'])) {
                        $data['material_id'] = $result['id'];
                    }
                    if (!empty($result['source'])) {
                        $data['source'] = $result['source'];
                    }

                    // 日志流水入库
                    $db->insert("data_logs_2018", $data);
                    $id = $db->insert_id();
                    echo "日志入库[$id]:{$db->last_query()}\n";

                    // 统计媒资UGC，meterial分享数
                    if ($data['log_operate'] == 'share') {
                        switch ($data['source']) {
                            case 'ugc':
                                // 查询UGC分享总数
                                $db->select("log_id");
                                $db->from("data_logs_2018");
                                $db->where(['log_uniqueid' => $result['uniqueid'], "log_operate" => "share", "material_id" => $result['id']]);
                                $count = $db->count_all_results();
                                // 修改分享总数
                                if (intval($count) > 0) {
                                    $re = $db->update("user_material", ['count_material_share' => $count], ['video_uuid' => $result['uniqueid'], "id" => $result['id']]);
                                    echo "[UGC分享统计] 总数({$count})-修改状态({$re}):{$db->last_query()}\n";
                                }
                                break;
                            case 'material':
                                // 查询Material分享总数
                                $db->select("log_id");
                                $db->from("data_logs_2018");
                                $db->where(['log_uniqueid' => $result['uniqueid'], "log_operate" => "share", "material_id" => $result['id']]);
                                $count = $db->count_all_results();
                                // 修改分享总数
                                if (intval($count) > 0) {
                                    $re = $db->update("video", ['count_material_share' => $count], ['video_uuid' => $result['uniqueid'], "video_id" => $result['id']]);
                                    echo "[Material分享统计] 总数({$count})-修改状态({$re}):{$db->last_query()}\n";
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            sleep(1);
        }
        sleep(1);
    }
} catch (Exception $e) {
    $process->log($e->getMessage());
}
